<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>资源列表</title>
    <%@include file="../../include/include.jsp" %>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="username" placeholder="请输入用户名" autocomplete="off"
                                   class="layui-input">
                        </div>
                        <input type="hidden" name="pageNum" value="1">
                        <input type="hidden" name="pageSize" value="${pageInfo.pageSize}">
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="sreach"><i
                                    class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-header">
                    <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除
                    </button>
                    <button class="layui-btn" onclick="xadmin.open('添加用户','${ctx}/resource/add','','',true)"><i
                            class="layui-icon"></i>添加
                    </button>
                </div>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>
                                <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                            </th>
                            <th>资源名</th>
                            <th>资源码</th>
                            <th>权限</th>
                            <th>种类</th>
                            <th>父节点</th>
                            <th>地址</th>
                            <th>图标</th>
                            <th>排序</th>
                            <th>状态</th>
                            <th>创建人</th>
                            <th>是否停用</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <script>
                            console.log(${pageInfo.list})
                        </script>
                        <c:forEach items="${pageInfo.list}" var="item">

                            <tr>
                                <td>
                                    <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                                </td>
                                <td>${item.resourceName}</td>
                                <td>${item.resourceCode}</td>
                                <td>${item.perm}</td>
                                <td>${item.type}</td>
                                <td>${item.parentId}</td>
                                <td>${item.url}</td>
                                <td>${item.icon}</td>
                                <td>${item.sort}</td>
                                <td>${item.status}</td>
                                <td>${item.createdBy}</td>

                                <td class="td-status">
                                    <span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span>
                                </td>
                                <td class="td-manage">
                                    <a onclick="member_stop(this,'10001')" href="javascript:;" title="启用">
                                        <i class="layui-icon">&#xe601;</i>
                                    </a>
                                    <a title="编辑" onclick="xadmin.open('编辑','${ctx}/resource/edit/'+${item.resourceId},600,400)"
                                       href="javascript:;">
                                        <i class="layui-icon">&#xe642;</i>
                                    </a>
                                    <a onclick="xadmin.open('修改密码','member-password.html',600,400)" title="修改密码"
                                       href="javascript:;">
                                        <i class="layui-icon">&#xe631;</i>
                                    </a>
                                    <a title="删除" onclick="member_del(this,${item.resourceId})" href="javascript:;">
                                        <i class="layui-icon">&#xe640;</i>
                                    </a>
                                </td>
                            </tr>
                        </c:forEach>

                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div id="page" class="page">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['laydate', 'form', 'laypage'], function () {
        var laydate = layui.laydate;
        var form = layui.form;

        // 监听全选
        form.on('checkbox(checkall)', function (data) {
            if (data.elem.checked) {
                $('tbody input').prop('checked', true);
            } else {
                $('tbody input').prop('checked', false);
            }
            form.render('checkbox');
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });

        var laypage = layui.laypage;

        //执行一个laypage实例
        laypage.render({
            elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
            //数据总数，从服务端得到
            , count: ${pageInfo.total},
            limit:${pageInfo.pageSize},
            curr:${pageInfo.pageNum},
            jump(obj, first) {
                //首次不执行
                if (!first) {
                    // 做一个界面跳转
                    window.location.href = "${ctx}/role/list?pageNum=" + obj.curr + "&pageSize=" + obj.limit;
                }
            }
        });

    });

    /*用户-停用*/
    function member_stop(obj, id) {
        layer.confirm('确认要停用吗？', function (index) {

            if ($(obj).attr('title') == '启用') {

                //发异步把用户状态进行更改
                $(obj).attr('title', '停用')
                $(obj).find('i').html('&#xe62f;');

                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                layer.msg('已停用!', {icon: 5, time: 1000});

            } else {
                $(obj).attr('title', '启用')
                $(obj).find('i').html('&#xe601;');

                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                layer.msg('已启用!', {icon: 5, time: 1000});
            }

        });
    }

    /*用户-删除*/
    function member_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            //发异步删除数据
            $.ajax({
                url: "${ctx}/resource/delete/" + id,
                type: "DELETE",
                success(res) {
                    if (res.success) {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    } else {
                        layer.msg(res.msg);
                    }
                }
            })

        });
    }


    function delAll(argument) {
        var ids = [];

        // 获取选中的id
        $('tbody input').each(function (index, el) {
            if ($(this).prop('checked')) {
                ids.push($(this).val())
            }
        });

        layer.confirm('确认要删除吗？' + ids.toString(), function (index) {
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
            $(".layui-form-checked").not('.header').parents('tr').remove();
        });
    }
</script>
</html>
